﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Com.Crc.ResManage.BLL.EquipManage;
using Com.Crc.PoJo.Model.Resource;
using Com.App_Code;
using Infragistics.WebUI.UltraWebGrid;

namespace Com.Crc.ResManage.Web.EquipManage
{
    public partial class BroadBandManageSub : BasePage
    {
        BroadBandManageBLL bll;
        BroadBandManageUserBoardBLL bllUserBoard;

        public string EquipID
        {
            get
            {
                if (ViewState["EquipID"] != null)
                {
                    return ViewState["EquipID"].ToString();
                }

                return "";
            }
            set
            {
                ViewState["EquipID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {   
                InitControl();
            }
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            this.btnAddUserBoard.Attributes.Add("onclick", "return OpenAddWindow();");
            this.btnDeleteUserBoard.Attributes.Add("onclick", "return window.confirm('确认删除所选用户板信息?')");
            if (Request.QueryString["equipid"] != null)
            {
                EquipID = Request.QueryString["equipid"].ToString();
            }
            //初始化各个下拉框
            InitControlHelp.LoadBranchInfo(this.ddlOffice);
            InitControlHelp.LoadCommonDicInfo(this.ddlBranch,Dictionary.COMMON_DIC_TYPE.RESOURCE_WLAN_BRANCH_TYPE);
            InitControlHelp.LoadCommonDicInfo(this.ddlEquipType, Dictionary.COMMON_DIC_TYPE.RESOURCE_WLAN_EQUIPMENT_TYPE);
            InitControlHelp.LoadCommonDicInfo(this.ddlUpEquip, Dictionary.COMMON_DIC_TYPE.RESOURCE_WLAN_UP_EQUIPMENT_TYPE);
            InitControlHelp.LoadCommonDicInfo(this.ddlBas, Dictionary.COMMON_DIC_TYPE.RESOURCE_WLAN_UP_BAS_TYPE);
            InitControlHelp.LoadCommonDicInfo(this.ddlUplevel, Dictionary.COMMON_DIC_TYPE.RESOURCE_WLAN_UP_LEVEL_TYPE);
            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                LoadEquipData();
                LoadUserBoardData();
            }
        }

        /// <summary>
        /// 修改设备信息时初始化控件数据
        /// </summary>
        private void LoadEquipData()
        {
            bll = new BroadBandManageBLL();
            DataTable dt = bll.QueryWLANData("", "", Request.QueryString["equipid"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlBas, dt.Rows[0]["N_BAS_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlBranch, dt.Rows[0]["N_DEP_BRANCH_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlEquipType, dt.Rows[0]["N_UNITTYPE_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlOffice, dt.Rows[0]["N_DEP_BRANCH_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlUpEquip, dt.Rows[0]["N_M_UP_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlUplevel, dt.Rows[0]["N_UP_LEVEL"].ToString());
            this.txtAddress.Text = dt.Rows[0]["S_ADDRESS"].ToString();
            this.txtBasPort.Text = dt.Rows[0]["N_BAS_PORT"].ToString();
            this.txtEquipName.Text = dt.Rows[0]["S_NAME"].ToString();
            this.txtOfficeNo.Text = dt.Rows[0]["S_NO"].ToString();
            this.txtQnqvlan.Text = dt.Rows[0]["N_QnQVLAN"].ToString();
            this.txtRemark.Text = dt.Rows[0]["S_BAK"].ToString();
            this.txtUpEquipPort.Text = dt.Rows[0]["N_M_UP_PORT"].ToString();
            this.txtVlan.Text = dt.Rows[0]["N_VLAN"].ToString();
        }

        /// <summary>
        /// 加载设备下用户板信息
        /// </summary>
        private void LoadUserBoardData()
        {
            bllUserBoard = new BroadBandManageUserBoardBLL();
            DataTable dt = bllUserBoard.QueryWLANUserBoardData("","",this.EquipID,"");
            BindData(dt);
        }

        private void BindData(DataTable dt)
        {
            grd.DataSource = dt;
            grd.DataBind();
        }

        /// <summary>
        /// 新增或修改保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            bll = new BroadBandManageBLL();
            WLANEntity wlanData = new WLANEntity();
            wlanData.N_DEP_BRANCH_ID = long.Parse(ddlBranch.Items[this.ddlBranch.SelectedIndex].Value);
            wlanData.N_SUBSTATION_ID = long.Parse(ddlOffice.Items[this.ddlOffice.SelectedIndex].Value);
            wlanData.N_UNITTYPE_ID = long.Parse(ddlEquipType.Items[this.ddlEquipType.SelectedIndex].Value);
            wlanData.S_NAME = txtEquipName.Text.Trim();
            wlanData.S_ADDRESS = txtAddress.Text.Trim();
            wlanData.N_VLAN = long.Parse(txtVlan.Text.Trim());
            wlanData.N_QnQVLAN = long.Parse(txtQnqvlan.Text.Trim());
            wlanData.N_BAS_ID = long.Parse(ddlBas.Items[this.ddlBas.SelectedIndex].Value);
            wlanData.N_BAS_PORT = long.Parse(txtBasPort.Text.Trim());
            wlanData.N_UP_LEVEL = long.Parse(ddlUplevel.Items[this.ddlUplevel.SelectedIndex].Value);
            wlanData.N_M_UP_ID = long.Parse(ddlUpEquip.Items[this.ddlUpEquip.SelectedIndex].Value);
            wlanData.N_M_UP_PORT = long.Parse(txtUpEquipPort.Text.Trim());
            wlanData.S_M_NAME = ddlUpEquip.Items[this.ddlUpEquip.SelectedIndex].Text;
            wlanData.S_BAK = txtRemark.Text.Trim();
            wlanData.S_STATE = "1";
            wlanData.D_TIME = DateTime.Now;

            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                //修改
                wlanData.N_ID = long.Parse(this.EquipID);
                bll.UpdateWLANData(wlanData);
                base.ShowAlertForUpdatePanel("设备修改成功！");
            }
            else
            {
                //新增
                bll.AddWLANData(wlanData);
                base.ShowAlertForUpdatePanel("设备新增成功！");
                this.btnAdd.Enabled = false;
            }
        }

        protected void grd_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs e)
        {
            grd.Bands[0].Columns.Clear();

            int i = 0;
            base.AddGridCheckColumn(this.grd, i++, grd.ClientID);
            base.AddGridColumn(this.grd, "N_ID", "N_ID", 80, i++, true);
            base.AddGridColumn(this.grd, "用户板数量", "N_CAP", 90, i++, false);
            base.AddGridColumn(this.grd, "用户板类型", "N_TYPE", 100, i++, false);
            base.AddGridColumn(this.grd, "起始端口号", "N_PORT_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束端口号", "N_PORT_E", 80, i++, false);
            base.AddGridColumn(this.grd, "起始槽位号", "N_TREN_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束槽位号", "N_TREN_E", 80, i++, false);
            base.AddGridColumn(this.grd, "起始Vlan", "N_VLAN_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束Vlan", "N_VLAN_E", 80, i++, false);

            base.AddGridImageColumn(grd, "编辑", "Edit", 40, i++, "~/img/icon_edit.gif");

            this.grd.Columns.FromKey("N_CAP").Width = Unit.Percentage(100);
        }

        protected void grd_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
        {

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.txtAddress.Text = "";
            this.txtBasPort.Text = "";
            this.txtEquipName.Text = "";
            this.txtOfficeNo.Text = "";
            this.txtQnqvlan.Text = "";
            this.txtRemark.Text = "";
            this.txtUpEquipPort.Text = "";
            this.txtVlan.Text = "";
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            LoadUserBoardData();
        }

        protected void btnDeleteUserBoard_Click(object sender, EventArgs e)
        {
            ArrayList arrUserboardID = new ArrayList();
            foreach (UltraGridRow gvRow in this.grd.DisplayLayout.Rows)
            {
                if (gvRow.Cells.FromKey("CHECK").ToString().ToUpper() == "TRUE")
                {
                    int ID = int.Parse(gvRow.Cells.FromKey("n_id").ToString());
                    arrUserboardID.Add(ID.ToString());
                }
            }

            if (arrUserboardID.Count == 0)
            {
                ShowAlertForUpdatePanel("请选择用户板!");
                return;
            }

            bllUserBoard = new BroadBandManageUserBoardBLL();
            bllUserBoard.DeleteWLANUserBoardData(arrUserboardID);
            LoadUserBoardData();
            ShowAlertForUpdatePanel("用户板删除成功!");
        }
    }
}